<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="品牌管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="品牌名称" prop="brandName">
                <el-input v-model="formData.brandName"/>
            </el-form-item>
            <el-form-item label="品牌logo" prop="logo">
                <el-upload
                    class="avatar-uploader"
                    :action="apiHost + '/file/upload'"
                    :show-file-list="false"
                    :on-success="handleSuccess"
                    :headers="headers"
                >
                    <div v-if="formData.logo">
                        <img :src="formData.logo" class="avatar">
                    </div>
                    <i v-else class="el-icon-plus avatar-uploader-icon"/>
                </el-upload>
                <div class="form-tips">尺寸：100*100</div>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {BrandApi} from '@/api/mall/brand'
import {Common} from '@/utils/common'
import { getToken } from '@/utils/auth'

export default {
    name: 'MallBrandUpdate',
    components: {},
    data() {
        return {
            opType: '添加',
            formData: {
                id: 0,
                brandName: '',
                logo: '',
            },
            rules: {
                brandName: [
                    {required: true, message: '请输入品牌名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            apiHost: process.env.VUE_APP_BASE_API,
            headers: { 'X-Token': getToken() }
        }
    },
    created() {

    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = 0
                }
            })
        },
        handleSuccess(e) {
            this.formData.logo = e.data
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        BrandApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        BrandApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>

<style lang="scss">

</style>
